package com.nuanshui.heatedloan.task.failover;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import com.nuanshui.heatedloan.task.facade.bean.JobParams;
/**
 * 任务执行失败时发送到失败队列的消息数据承载类
 * @param 
 * @author guohao
 * @date 2017年2月13日 上午9:47:50
 * @since $
 */
public class TaskFailMessage implements Serializable {
	
	private JobParams jobParams;//任务参数
	
	private String jobGroup;//任务组
	
	private String jobName;//任务名
	
	private Date failTime;//失败时间
	
	private Exception  failReason;//失败原因
	
	private int num = 0;//执行次数
		
	private String failover;//失败策略
	
	private String code = null;//任务 code
	
	
	private int retries = -1;//重试次数
	
	
	
	public void setCode(String code) {
		this.code = code;
	}

	public String getFailover() {
		return failover;
	}

	public void setFailover(String failover) {
		this.failover = failover;
	}

	public int getRetries() {
		return retries;
	}

	public void setRetries(int retries) {
		this.retries = retries;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public JobParams getJobParams() {
		return jobParams;
	}

	public void setJobParams(JobParams jobParams) {
		this.jobParams = jobParams;
	}

	public String getJobGroup() {
		if(jobGroup == null) jobGroup = this.jobParams.getJobGroup();
		return jobGroup;
	}

	public void setJobGroup(String jobGroup) {
		this.jobGroup = jobGroup;
	}

	public String getJobName() {
		if(jobName == null) jobName = this.jobParams.getJobName();
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public Date getFailTime() {
		return failTime;
	}

	public void setFailTime(Date failTime) {
		this.failTime = failTime;
	}

	public Exception getFailReason() {
		return failReason;
	}

	public void setFailReason(Exception failReason) {
		this.failReason = failReason;
	}
	
	
	public String   getCode() {
		 if(code !=null) return code;
		 int multiplier = 37;
		 int hashcode = 17;
		 long checksum = 0;
		 int count = 0;
		 if(jobParams != null) {
			 Set<String> keys = jobParams.keySet();
			 for(String key :keys){
				 Object object = jobParams.get(key);
				 int baseHashCode = object == null ? 1 : object.hashCode();
				    count++;
				    checksum += baseHashCode;
				    baseHashCode *= count;
				    hashcode = multiplier * hashcode + baseHashCode;
			 }
		 }		 
		 StringBuilder sb = new StringBuilder();
		 code =  sb.append(multiplier).append(hashcode).append(checksum).append(count).toString();
		 return code;
	}
	
	public static void main(String[] args) {
		TaskFailMessage taskFailMessage = new TaskFailMessage();
		JobParams jobParams = new JobParams();
		jobParams.setJobGroup("test");
		jobParams.setJobName("test");
		jobParams.addAttribute("museumId","124");
        taskFailMessage.setJobParams(jobParams);
		System.out.println(taskFailMessage.getCode());
	}
	
}
